package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PatchGrouponStockLmtEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.DiscountProductSingleDetailMapper;
import com.jic.market.mapper.PatchGrouponStockLmtEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import com.jic.market.request.rest.discount.DiscountCancelRequest;
import com.jic.market.request.rest.discount.DiscountOrderCreateRequest;
import com.jic.market.request.rest.discount.DiscountSubOrderRequest;
import com.jic.market.request.rest.groupon.CancelPayGroupInstDetailRequest;
import com.jic.market.request.rest.groupon.LaunchOrJoinGrouponRequest;
import com.jic.market.response.rest.groupon.LaunchOrJoinGrouponResponse;
import com.jic.market.response.rest.groupon.PostPayGroupInstDetailResponse;
import com.jic.market.service.rest.discount.GiftService;
import com.jic.market.service.rest.groupon.GrouponRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 拼团
 * @author yinhaijun
 * @date: 2020/5/12
 */
@Service("GrouponDiscountProcessor")
public class GrouponDiscountProcessor extends AbstractSingleDiscountProcessor {

    @Autowired
    private PatchGrouponStockLmtEntityMapper patchGrouponStockLmtEntityMapper;
    @Autowired
    private GrouponRestService grouponRestService;
    @Autowired
    private DiscountProductSingleDetailMapper discountProductSingleDetailMapper;
    @Autowired
    private GiftService giftService;
    @Override
    public SingleResult isCondition(GoodsDetailModel goodsDetailModel, DiscountBaseRequest request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(request.getThirdUserId(), request.getAdminOperate().getChildId().toString(), goodsDetailModel.getChannelCodes(), goodsDetailModel.getPromotionMemberGradeList())){
            return new SingleResult(false, "拼团计算"+goodsDetailModel.getPromotionCode()+"会员等级校验未通过");
        }
        PatchGrouponStockLmtEntity patchGrouponStockLmtEntity = patchGrouponStockLmtEntityMapper.getEntityByPromotionIdAndMpId(Long.parseLong(goodsDetailModel.getPromotionId()), goodsDetailModel.getSkuId());
        if(null == patchGrouponStockLmtEntity || null == patchGrouponStockLmtEntity.getGrouponPrice()){
            SingleResult singleResult = new SingleResult(false, "团购价找不到");
            return singleResult;
        }
        //
//        Integer orderLimit = patchGrouponStockLmtEntity.getOrderLimit();
//        if(goodsDetailModel.getNum() > orderLimit){
//            SingleResult singleResult = new SingleResult(false, "单笔超限");
//            return singleResult;
//        }
        Long individualLimit = patchGrouponStockLmtEntity.getIndividualLimit();
        Integer userCountNum = discountProductSingleDetailMapper.countSingleValidNumByPromotionIdAndThirdSkuIdAndThirdUserId(goodsDetailModel.getPromotionId(), goodsDetailModel.getThirdSkuId(), request.getThirdUserId());
        Long channelStoreLimit = patchGrouponStockLmtEntity.getChannelStoreLimit();
        Integer countNum = discountProductSingleDetailMapper.countSingleValidNumNumByPromotionIdAndThirdSkuId(goodsDetailModel.getPromotionId(), goodsDetailModel.getThirdSkuId());
        Long usedNum = giftService.getUsedNum(goodsDetailModel.getNum().longValue(), channelStoreLimit, individualLimit, userCountNum.longValue(), countNum.longValue());
        goodsDetailModel.setSingleValidNum(usedNum);
        // 获得团购价
        BigDecimal grouponPrice = patchGrouponStockLmtEntity.getGrouponPrice();
        SingleResult singleResult = new SingleResult(true, "有优惠");
        singleResult.setObject(grouponPrice);
        return singleResult;
    }

    @Override
    public void queryShoppingCartAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request){
        // 优惠总价 = (原价 - 团购价)*数量
        querySettlementAfter(singleResult, goodsDetailModel, request);
    }
    @Override
    public void querySettlementAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request){
        // 优惠总价 = (原价 - 团购价)*数量
        BigDecimal single = Money.subtract(goodsDetailModel.getSalePrice(), (BigDecimal) singleResult.getObject());
        BigDecimal promotionDiscountTotalAmount =  Money.multiply(single, new BigDecimal(goodsDetailModel.getSingleValidNum()));
        goodsDetailModel.setPromotionDiscountTotalAmount(promotionDiscountTotalAmount);
    }


    @Override
    public void createAfter(GoodsDetailModel goodsDetailModel, Object request, String thirdUserId){
        DiscountSubOrderRequest grouponRequest = (DiscountSubOrderRequest) request;
        // 调用参团活动
        LaunchOrJoinGrouponRequest launchOrJoinGrouponRequest = new LaunchOrJoinGrouponRequest();
        launchOrJoinGrouponRequest.setGroupCode(goodsDetailModel.getPromotionCode());
        launchOrJoinGrouponRequest.setThirdProductCode(goodsDetailModel.getThirdSkuId());
        launchOrJoinGrouponRequest.setThirdOrderCode(grouponRequest.getOrderNo());
        launchOrJoinGrouponRequest.setBuyNum(goodsDetailModel.getSingleValidNum());
        launchOrJoinGrouponRequest.setGrouponInstCode(goodsDetailModel.getGrouponInstCode());
        launchOrJoinGrouponRequest.setLaunchFlag(goodsDetailModel.getLaunchFlag());
        launchOrJoinGrouponRequest.setThirdUserId(thirdUserId);
        LaunchOrJoinGrouponResponse launchOrJoinGrouponResponse = grouponRestService.launchOrJoinGroupon(launchOrJoinGrouponRequest);
        goodsDetailModel.setLaunchOrJoinSuccess(launchOrJoinGrouponResponse.getLaunchOrJoinSuccess());
        goodsDetailModel.setGrouponInstCode(launchOrJoinGrouponResponse.getGrouponInstCode());
    }

    @Override
    public void cancelAfter(GoodsDetailModel goodsDetailModel, Object request){
        DiscountCancelRequest discountCancelRequest = (DiscountCancelRequest) request;
        CancelPayGroupInstDetailRequest cancelPayGroupInstDetailRequest = new CancelPayGroupInstDetailRequest();
        cancelPayGroupInstDetailRequest.setThirdUserId(discountCancelRequest.getThirdUserId());
        cancelPayGroupInstDetailRequest.setThirdOrderCode(discountCancelRequest.getOrderNo());
        PostPayGroupInstDetailResponse launchOrJoinGrouponResponse = grouponRestService.cancelPayGroupInstDetail(cancelPayGroupInstDetailRequest);
        if(null != launchOrJoinGrouponResponse){
            goodsDetailModel.setGroupInstDetailInfoList(launchOrJoinGrouponResponse.getDetailInfoList());
        }
    }


}
